<template>
  <div class="app-container">
    <el-button type="success" icon="el-icon-finished" style="margin: 10px;" @click="handleSubmit">
      提交
    </el-button>
    <el-button type="info" icon="el-icon-refresh" style="margin: 10px;" @click="handleRefresh">
      重置
    </el-button>
    <el-button type="info" icon="el-icon-circle-close" style="margin: 10px;" @click="handleClose">
      关闭
    </el-button>

    <el-form ref="productAddForm" :rules="productRules" :model="product" label-width="80px" style="margin-left:0px;">
      <el-form-item label="货号" prop="sku">
        <el-input v-model="product.sku" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="品牌" prop="brandId">
        <el-select v-model="product.brandId" clearable filterable>
          <el-option v-for="item in brandOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input v-model="product.name" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="系列" prop="series">
        <el-input v-model="product.series" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="大类" prop="goodsCategory">
        <el-select v-model="product.goodsCategory" clearable filterable>
          <el-option v-for="item in goodsCategoryOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-select v-model="product.year" clearable filterable>
          <el-option v-for="item in yearOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="季节" prop="season">
        <el-select v-model="product.season" clearable filterable>
          <el-option v-for="item in seasonOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="goodsSex">
        <el-select v-model="product.goodsSex" clearable filterable>
          <el-option v-for="item in goodsSexOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-input v-model="product.color" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="重量(KG)" prop="weight">
        <el-input v-model="product.weight" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="吊牌价" prop="marketPrice">
        <el-input v-model="product.marketPrice" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="卖点" prop="saleProps">
        <el-input v-model="product.saleProps" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="材质" prop="material">
        <el-input v-model="product.material" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="细节备注" prop="remark">
        <el-input v-model="product.remark" style="width: 200px;" />
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { addProduct } from '@/api/manager/goods-api'
import { toOptionsList } from '@/utils/index'

export default {
  name: 'ManagerProductAdd',
  filters: {
  },
  data() {
    return {
      brandOptions: [],
      goodsCategoryOptions: [],
      yearOptions: [],
      seasonOptions: [],
      goodsSexOptions: [],
      product: {
        sku: null,
        brandId: null,
        name: null,
        series: null,
        goodsCategory: null,
        year: null,
        season: null,
        goodsSex: null,
        color: null,
        weight: null,
        marketPrice: null,
        saleProps: null,
        material: null,
        remark: null
      },
      productRules: {
        sku: [{
          required: true, message: '请输入货号', trigger: 'blur'
        }],
        brandId: [{
          required: true, message: '请输入品牌', trigger: 'blur'
        }],
        goodsCategory: [{
          required: true, message: '请输入大类', trigger: 'blur'
        }],
        year: [{
          required: true, message: '请输入年份', trigger: 'blur'
        }],
        season: [{
          required: true, message: '请输入季节', trigger: 'blur'
        }],
        goodsSex: [{
          required: true, message: '请输入性别', trigger: 'blur'
        }],
        weight: [{
          required: true, message: '请输入重量', trigger: 'blur'
        }],
        marketPrice: [{
          required: true, message: '请输入吊牌价', trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    await this.initLoad()
    await this.loadDetail()
  },
  methods: {
    async initLoad() {
      const dataDictsParam = ['BRAND', 'GOODS_CATEGORY', 'YEAR', 'SEASON', 'GOODS_SEX']
      const { data } = await fetchEnum(dataDictsParam)
      for (const dataDict of data) {
        if (dataDict.code === 'BRAND') {
          if (dataDict.options) {
            this.brandOptions = toOptionsList(dataDict.options, 'id', 'shortName')
          }
        } else if (dataDict.code === 'GOODS_CATEGORY') {
          if (dataDict.options) {
            this.goodsCategoryOptions = toOptionsList(dataDict.options, 'code', 'name')
          }
        } else if (dataDict.code === 'YEAR') {
          if (dataDict.options) {
            this.yearOptions = toOptionsList(dataDict.options, 'code', 'name')
          }
        } else if (dataDict.code === 'SEASON') {
          if (dataDict.options) {
            this.seasonOptions = toOptionsList(dataDict.options, 'code', 'name')
          }
        } else if (dataDict.code === 'GOODS_SEX') {
          if (dataDict.options) {
            this.goodsSexOptions = toOptionsList(dataDict.options, 'code', 'name')
          }
        }
      }
    },
    loadDetail() {
      this.product = {
        sku: null,
        brandId: null,
        name: null,
        series: null,
        goodsCategory: null,
        year: null,
        season: null,
        goodsSex: null,
        color: null,
        weight: null,
        marketPrice: null,
        saleProps: null,
        material: null,
        remark: null
      }
    },

    handleSubmit() {
      this.$refs.productAddForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.product))
        addProduct(data).then(response => {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleRefresh() {
      this.loadDetail()
    },
    handleClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }

  }
}

</script>
